package org.easymis.oa.modules.main.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.easymis.entitys.mybatis.dto.FlowRun;
import org.easymis.entitys.mybatis.dto.FlowType;
import org.easymis.entitys.mybatis.dto.Member;
import org.easymis.entitys.mybatis.dto.OaAttendOvertime;
import org.easymis.entitys.mybatis.dto.OrganizeFunction;
import org.easymis.oa.service.datacenter.FlowRunMng;
import org.easymis.oa.service.datacenter.FlowTypeMng;
import org.easymis.oa.service.datacenter.OrganizeFunctionMng;
import org.easymis.oa.service.main.OaAttendOvertimeMng;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
 * 
 * @see 节假日加班
 *
 */
@Controller
@RequestMapping("/attend/overtime/")
public class OaAttendOvertimeAction {
	@Resource
	private OaAttendOvertimeMng service;
	@Resource
	private FlowRunMng flowRunMng;
	@Resource
	private FlowTypeMng flowTypeMng;
	@Autowired
	OrganizeFunctionMng organizeFunctionMng;

	@RequestMapping(value = "index")
	public String forwardaddressareaIndexJsp(HttpServletRequest request, HttpSession session,ModelMap model) throws Exception {
		String functionId=request.getParameter("menuId");
		List<OrganizeFunction> breadcrumbList= organizeFunctionMng.getBreadcrumb(functionId);
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		model.put("breadcrumbList", breadcrumbList);
		return "attend/overtime/index";
	}

	@RequestMapping(value = "grid", method = RequestMethod.POST)
	public String forwardList(int page, int rows, String flowTypeId,HttpServletRequest request) throws Exception {
		OaAttendOvertime bean = new OaAttendOvertime();
		//bean.setOrgId(orgId);
		List overtimeList=service.getList(bean);
		
		FlowRun flowRun = new FlowRun();
		flowRun.setFlowTypeId(flowTypeId);
		List<FlowRun> flowRun_list=flowRunMng.getList(flowRun);
		
		request.setAttribute("overtimeList", overtimeList);
		request.setAttribute("flowRun_list", flowRun_list);
		return "attend/overtime/grid";
	}

	@RequestMapping(value = "forwordEdit")
	public String forwardEdit(HttpServletRequest request, HttpSession session) throws Exception {
		Member userInfo =(Member)session.getAttribute("userInfo");
		String sysName=request.getParameter("sysName");
		FlowType flowType=flowTypeMng.findBySysName(sysName);		
		String flow_name=flowType.getTypeName()+"-"+userInfo.getStaffName()+"-"+"2017-11-06";
		
		request.setAttribute("base", "http://demo.easymis.cn/oa");
		request.setAttribute("flowTypeId", flowType.getFlowTypeId());
		request.setAttribute("flowName", flow_name);
		return "attend/overtime/edit";
	}

	@RequestMapping(value = "forwordUpdate")
	public String forwardUpdate(HttpServletRequest request, HttpSession session) throws Exception {
		return "attend/overtime/update";
	}

	@RequestMapping(value = "forwordView")
	public String forwardView(HttpServletRequest request, HttpSession session) throws Exception {
		return "attend/overtime/view";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public void save(HttpServletRequest request, HttpSession session, OaAttendOvertime bean) throws Exception {
		Member userInfo =(Member)session.getAttribute("userInfo");
		if (bean.getBeginTime() == null) {

		}
		String flowTypeId=request.getParameter("flowTypeId");
		String flowName=request.getParameter("flowName");

		FlowRun flowRun = new FlowRun();
		flowRun.setFlowName(flowName);
		flowRun.setFlowTypeId(flowTypeId);
		flowRun.setCreatorId(userInfo.getStaffId());		
		flowRun.setCreateTime(new Date());		
		flowRun.setOrgId(userInfo.getOrgId());
		flowRunMng.save(flowRun);
		

		bean.setCreatorId(userInfo.getStaffId());
		bean.setAttendOvertimeId(flowRun.getFlowRunId());
		bean.setOrgId(userInfo.getOrgId());
		bean.setStaffId(userInfo.getStaffId());
		service.save(bean);

	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public void updateaddressarea(HttpServletRequest request, HttpSession session) throws Exception {
		String id = request.getParameter("id");
		OaAttendOvertime bean = service.findById(id);
		service.update(bean);
	}

	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public void delete(HttpServletRequest request, String id) throws Exception {
		service.delete(id);
	}

	@RequestMapping(value = "deletes", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public void deletes(HttpServletRequest request) throws Exception {

	}
}
